package com.yz.controller;

import com.yz.dao.EmployeeDao;
import com.yz.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

/**
 * @author yuanzheng
 * @date 2020/7/25-17:08
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeDao employeeDao;

    @RequestMapping("/list")
    public String list(Model model) {
        Collection<Employee> employees = employeeDao.getEmployees();
        model.addAttribute("employees", employees);
        return "emp/list";
    }

    @GetMapping("/add")
    public String add() {
        return "emp/add";
    }

    @PostMapping("/add")
    public String addEmpolyee(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/employee/list";
    }
}
